//
//  DDJobCategoryDetailViewController.m
//  DingDongZP
//
//  Created by Jyh on 16/4/23.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDJobCategoryDetailViewController.h"
#import "DDJobCategoryService.h"
#import "DDJobCategoryModel.h"

#import "SKTagView.h"
#import "DDBossPublishJobViewController.h"

@interface DDJobCategoryDetailViewController () <UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) DDJobCategoryService *jobService;

@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) SKTagView *tagView;

@property (strong, nonatomic) NSMutableArray *dataSource;

@property (strong, nonatomic) NSMutableArray *tagTitleArray;

/**
 *  二级职位ID，用于技能要求的请求
 */
@property (copy, nonatomic) NSString *level2ID;

@end

@implementation DDJobCategoryDetailViewController

#pragma mark
#pragma mark - View LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.jobService = [[DDJobCategoryService alloc] init];
    
    [self setupView];
    [self setupData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark
#pragma mark - Private Methods

- (void)setupView
{
    WEAKSELF
    
    UIScrollView *tagScrollView = [[UIScrollView alloc] init];
    
    [self.view addSubview:tagScrollView];
    
    [self.view addSubview:self.tableView];
    [tagScrollView addSubview:self.tagView];
    
    _tagView.didTapTagAtIndex = ^(NSUInteger index){
        
        if ([weakSelf.dataSource count] > index) {

            DDJobCategoryThird *tag = (DDJobCategoryThird *)weakSelf.dataSource[index];
            
            NSLog(@"选中的职位_  %@___%@",tag.level3_name,tag.level3_id);

            NSMutableArray *navigationArray = [[NSMutableArray alloc] initWithArray: weakSelf.navigationController.viewControllers];
            if ([navigationArray count] > 3) {
                id vc = navigationArray[navigationArray.count - 3];
                if ([vc isKindOfClass:[DDBossPublishJobViewController class]]) {
                    DDBossPublishJobViewController *jobVc = (DDBossPublishJobViewController *)vc;
                    jobVc.infoModel.category_id = tag.level3_id;
                    jobVc.infoModel.category_name = tag.level3_name ;
                    jobVc.infoModel.lever2ID = weakSelf.level2ID;
                    [jobVc realoadTableView];
                }
                
                [navigationArray removeObjectAtIndex:[navigationArray count] - 2];            }

            weakSelf.navigationController.viewControllers = navigationArray;
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakSelf.navigationController popViewControllerAnimated:YES];
            });
        }
    };
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    // 设置tableview默认选中第一行
    NSIndexPath *first = [NSIndexPath
                          indexPathForRow:0 inSection:0];
    
    [self.tableView selectRowAtIndexPath:first
                                animated:YES
                          scrollPosition:UITableViewScrollPositionTop];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.view.mas_left);
        make.top.mas_equalTo(weakSelf.view.mas_top);
        make.bottom.mas_equalTo(weakSelf.view.mas_bottom);
        make.width.mas_equalTo(85);
    }];
    
    [tagScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.tableView.mas_right);
        make.top.mas_equalTo(weakSelf.view.mas_top);
        make.bottom.mas_equalTo(weakSelf.view.mas_bottom);
        make.right.mas_equalTo(weakSelf.view.mas_right);
    }];
    
    [self.tagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(tagScrollView.mas_left);
        make.top.mas_equalTo(tagScrollView.mas_top);
        make.bottom.mas_equalTo(tagScrollView.mas_bottom);
        make.right.mas_equalTo(tagScrollView.mas_right);
        make.width.mas_equalTo(kScreenWidth-85);
    }];
}

- (void)setupData
{
    if ([self.jobCategoryArray count] > 0) {
        DDJobCategorySecond *title = (DDJobCategorySecond *)self.jobCategoryArray[0];
        
        self.level2ID = [NSString stringWithFormat:@"%d",(int)title.level2_id];
//        [self rquestInfoWithLevelId:title.level2_id];
        [self loadLevel3ArrayWithIndex:0];
    }
}


- (void)loadLevel3ArrayWithIndex:(NSInteger)index
//- (void)rquestInfoWithLevelId:(NSInteger)levelTagId
{
    if (index > [self.jobCategoryArray count] - 1) {
        return;
    }
    
    WEAKSELF
    [self.dataSource removeAllObjects];
    
    DDJobCategorySecond *title = (DDJobCategorySecond *)self.jobCategoryArray[index];
    
    self.dataSource = [title.level3 mutableCopy];
    
    NSMutableArray *tagTitleArray = [@[] mutableCopy];
    for (int i = 0; i < [title.level3 count]; i ++) {
        DDJobCategoryThird *tag = (DDJobCategoryThird *)title.level3[i];
        [tagTitleArray addObject:tag.level3_name];

    }
    
    [weakSelf addTagWithInfoWithArray:tagTitleArray];
}

- (void)addTagWithInfoWithArray:(NSMutableArray *)tagArray
{
    //Add Tags
    if ([tagArray count] > 0) {
        [self.tagView removeAllSubviews];
        
        [tagArray enumerateObjectsUsingBlock:^(NSString *text, NSUInteger idx, BOOL * _Nonnull stop) {
            SKTag *tag = [SKTag tagWithText: text];
            tag.textColor = [UIColor darkGrayColor];
            tag.fontSize = 14;
            tag.borderColor = COLOR_BORDER_GRAY;
            tag.borderWidth = 1;

            tag.padding = UIEdgeInsetsMake(10, 10, 10, 10);

            tag.bgColor = [UIColor whiteColor];
            tag.cornerRadius = 5;
            [self.tagView addTag:tag];
        }];
    }
}

#pragma mark -
#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.jobCategoryArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cellIdentfier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        
    }
    if (indexPath.row < [self.jobCategoryArray count]) {
        DDJobCategorySecond *title = (DDJobCategorySecond *)self.jobCategoryArray[indexPath.row];
        cell.textLabel.text = title.level2_name;
        cell.textLabel.font = [UIFont systemFontOfSize:13];
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.highlightedTextColor = COLOR_FONT_MAIN_GREEN;
        cell.contentView.backgroundColor = COLOR_BACKGROUND_LIGHTGREEN;
    }
    
    
    return cell;
}

#pragma mark -
#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row < [self.jobCategoryArray count]) {
        DDJobCategorySecond *title = (DDJobCategorySecond *)self.jobCategoryArray[indexPath.row];
        
        self.level2ID = [NSString stringWithFormat:@"%d",(int)title.level2_id];
//        NSLog(@"______level2 ID %ld   name   %@",title.level2_id,title.level2_name);
        
        [self loadLevel3ArrayWithIndex:indexPath.row];
    }
    
}

#pragma mark
#pragma mark - Getters and Setters

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = COLOR_BACKGROUND;
    }
    
    return _tableView;
}

- (SKTagView *)tagView {
    if (!_tagView) {
        _tagView = [SKTagView new];
        _tagView.backgroundColor = [UIColor whiteColor];
        _tagView.padding = UIEdgeInsetsMake(10, 10, 10, 10);
        _tagView.interitemSpacing = 15;
        _tagView.lineSpacing = 10;
    }
    
    return _tagView;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [[NSMutableArray alloc] initWithCapacity:1];
    }
    return _dataSource;
}

- (NSMutableArray *)tagTitleArray {
    if (!_tagTitleArray) {
        _tagTitleArray = [[NSMutableArray alloc] initWithCapacity:1];
    }
    return _tagTitleArray;
}

@end
